package org.linlinjava.litemall.admin.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.AdminCashesService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCashes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.linlinjava.litemall.admin.service.LogHelper;
import javax.validation.constraints.NotNull;
import java.util.List;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.*;

/**
 * @Description:	提现管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-14 10:04:50
 */
@RestController
@RequestMapping("/admin/cashes")
@Validated
public class AdminCashesController {
    private final Log logger = LogFactory.getLog(AdminCashesController.class);

    @Autowired
    private AdminCashesService cashesService;

    @Autowired
    private LogHelper logHelper;

    @RequiresPermissions({"admin:cashes:list"})
    @RequiresPermissionsDesc(menu = {"商场管理", "提现管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCashes> litemallCashesList = cashesService.querySelective(name, page, limit, "add_time", "desc");
        return ResponseUtil.okList(litemallCashesList);
    }
    @RequiresPermissions("admin:cashes:read")
    @RequiresPermissionsDesc(menu = {"商场管理", "提现管理"}, button = "详情")
    @GetMapping("/detail")
    public Object litemallCashesDetail(@NotNull Integer id) {
    	LitemallCashes litemallCashes=cashesService.findById(id);
        return ResponseUtil.ok(litemallCashes);
    }

    @RequiresPermissions("admin:cashes:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "提现管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallCashes entity) {
        Object error = validate(entity);
        if (error != null) {
            return error;
        }
        try {
            cashesService.add(entity);
        } catch (Exception e) {
            return ResponseUtil.fail(STORE_TIXIA_ERROR, e.getMessage());
        }


        return ResponseUtil.ok();
    }

    @RequiresPermissions({"admin:cashes:approvallist"})
    @RequiresPermissionsDesc(menu = {"商场管理", "提现审批"}, button = "查询")
    @GetMapping("/approvallist")
    public Object approvallist(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCashes> litemallCashesList = cashesService.querySelective(name, page, limit, "add_time", "desc");
        return ResponseUtil.okList(litemallCashesList);
    }

    @RequiresPermissions("admin:cashes:approval")
    @RequiresPermissionsDesc(menu = {"商场管理", "提现审批"}, button = "审批")
    @PostMapping("/approval")
    public Object approval(@RequestBody LitemallCashes entity) {

        try {
            cashesService.approval(entity);
        } catch (Exception e) {
            return ResponseUtil.fail(STORE_TIXIA_APPROVAL_ERROR, e.getMessage());
        }
        return ResponseUtil.ok();
    }

    private Object validate(LitemallCashes entity) {
        String temp = entity.getName();
        if (StringUtils.isEmpty(temp)) {
            return ResponseUtil.badArgument();
        }
        temp = entity.getBankName();
        if (StringUtils.isEmpty(temp)) {
            return ResponseUtil.badArgument();
        }
        temp = entity.getCardNo();
        if (StringUtils.isEmpty(temp)) {
            return ResponseUtil.badArgument();
        }

        return null;
    }


}
